﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using Small.Html.Options;
using Small.Html.UI;
using Small.Html.Entities;
using SmallProject.Models.Test;
using SmallUtil;
using Small.Html.Enum;


namespace SmallProject.Controllers.Test
{
    public class TestController : Controller
    {
         List<TestModel> model = new List<TestModel>();

         public TestController()
         {
             model.Add(new TestModel { Key = 1, ParentID = 0, Name = "A", Age = 1, No = "A" });
             model.Add(new TestModel { Key = 2, ParentID = 1, Name = "B", Age = 2, No = "B" });
             model.Add(new TestModel { Key = 3, ParentID = 1, Name = "C", Age = 3, No = "C" });
             model.Add(new TestModel { Key = 4, ParentID = 2, Name = "D", Age = 4, No = "D" });
             model.Add(new TestModel { Key = 5, ParentID = 2, Name = "E", Age = 5, No = "E" });
             model.Add(new TestModel { Key = 6, ParentID = 3, Name = "F", Age = 6, No = "F" });
             model.Add(new TestModel { Key = 7, ParentID = 3, Name = "G", Age = 7, No = "H" });
             model.Add(new TestModel { Key = 8, ParentID = 4, Name = "H", Age = 8 });
             model.Add(new TestModel { Key = 9, ParentID = 5, Name = "J", Age = 9 });
             model.Add(new TestModel { Key = 10, ParentID = 6, Name = "L", Age = 10 });
             model.Add(new TestModel { Key = 11, ParentID = 0, Name = "W", Age = 11 });
             //
             // GET: /Index/
         }
        public ActionResult Index()
        {
           

            TreeList list=   GetTreeNode(model);
            foreach (var item in list.Nodes)
            {

                Dispay(item);
            }
            return View(model);
        }


        public ActionResult Index1() 
        {

          
            SysContext.FilterItem= SysContext.SysFilter.GetFilter(HttpContext);

            return Json("1");
        }
        private void Dispay(TreeNode node) 
        {
            if (node.ChildNodes == null) { return; }
            foreach (var item in node.ChildNodes)
            {
                Console.WriteLine(String.Format("{0}:{1}", item.Content.Key, item.Content.Parent));
                Dispay(item);
            }
        }



        private static TreeList GetTreeNode(List<TestModel> list)
        {
            TreeList treeList = new TreeList();
            treeList.Nodes = new List<TreeNode>();
            var parentNode = list.Where(p => p.ParentID == 0);
            foreach (var item in parentNode)
            {
                TreeNode node = new TreeNode();
                node.Content = new TreeItem { Key = Convert.ToString(item.Key), Parent = Convert.ToString(item.ParentID) };
                node.ChildNodes = new List<TreeNode>();
                treeList.Nodes.Add(node);
                GetNodeList(list, node);
                 
            }


            return treeList;
        }

        private static void GetNodeList(IEnumerable<TestModel> list, TreeNode node1)
        {

            var nodes = list.Where(p => Convert.ToString(p.ParentID).Equals(node1.Content.Key));
            foreach (var item2 in nodes)
            {
                TreeNode node2 = new TreeNode();
                node2.Content = new TreeItem { Key = Convert.ToString(item2.Key), Parent = Convert.ToString(item2.ParentID) };
                node1.ChildNodes = new List<TreeNode>();
                node1.ChildNodes.Add(node2);
                var nodes3 = list.Where(p => Convert.ToString(p.ParentID).Equals(node2.Content.Key));
                if (nodes3.Count() > 0)
                {

                    GetNodeList(list, node2);
                }

                
                
            }
        }


        [HttpGet]
        public ActionResult GridList() 
        {


            JqGridOptions jqGridOptions = new JqGridOptions();
            jqGridOptions.Url = "Test/List";
            jqGridOptions.Mtype =  FormMethodType.post;
            jqGridOptions.ColNames = new string[] { "Key", "No", "Age", "ParentID", "Name" };
            jqGridOptions.Datatype = DataType.json;
            jqGridOptions.ColModel = new JqGridColModel[]
            {
               new JqGridColModel{ Align=TextAlign.left, Name="Key", Sortable=true, Key=true },
               new JqGridColModel{ Align=TextAlign.right, Name="No", Sortable=true ,
                   Formatter=new JqGridColModeShowLinkFormat{ Target="_blank", BaseLinkUrl="Home/Index", IdName="No"} },
               new JqGridColModel{ Align=TextAlign.left, Name="Age", Sortable=true},
               new JqGridColModel{ Align=TextAlign.right, Name="ParentID", Sortable=true},
               new JqGridColModel{ Align=TextAlign.left, Name="Name", Sortable=true, Editable=true,
                        Editrules=new JqEditrule{ Integer=true}}
            };
            jqGridOptions.Pager = "#pager2";
            jqGridOptions.Sortname = "Name";
            jqGridOptions.RowNum = 10;
            jqGridOptions.CellEdit = true;
            jqGridOptions.Cellsubmit = JqCellSubmitType.clientArray;
            jqGridOptions.Viewrecords = true;
            jqGridOptions.Caption = "测试";
            jqGridOptions.Sortorder = OrderByType.asc;
            jqGridOptions.RowList = new Int32[] { 10, 20, 30 };
            ViewBag.JqGridOptions = jqGridOptions;
            
            return View();
        }


        [HttpPost]
        public ActionResult List() 
        {

            //jqGridOptions.ColNames = new string[] { "Key", "No", "Age", "ParentID", "Name" };
          
            Int32? page = 1;
            var jsonResult = new
            {
                page = page,
                total = model.Count / page,
                records = model.Count,
                rows = model.Select(p => new
                {
                      id = p.Key,
                      cell = new object[]{
                      p.Key,
                      p.No,
                      p.Age,
                      p.ParentID,
                      p.Name,
                     
                    }
                }
                )
            };

            
            return Json(jsonResult);
        }


    }
}
